package org.example.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Auther:Liu
 * @Testname:UserAdvice
 * @Date:2025/7/30 9:40
 */
@Component
@Aspect

public class UserAdvice {

    @Pointcut("execution(* org.example.target.*.*(..))")
    private void excute() {
    }

    //    不获取目标对象参数(后置通知)
    @AfterReturning("excute()")
    public void getadvice() {
        System.out.println("奖励100￥");
    }

    //    获取参数
    @AfterReturning("execution(* org.example.target.*.*(..))")
    public void getadvice2(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        System.out.println("后置捕获姓名为" + Arrays.toString(args));
    }

    //    前置
    @Before("excute()")
    public void before(JoinPoint joinPoint) {
        System.out.println("我是前置通知");
    }

    //    最终通知
    @After("excute()")
    public void fina() {
        System.out.println("我是最终通知");
    }

    //环绕通知
    @Around("excute()")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕之前.......");
        Object proceed = joinPoint.proceed();
        System.out.println("环绕" + proceed);
        System.out.println("环绕之后.......");
    }

//    异常通知
    @AfterThrowing("excute()")
    public void exception() {
        System.out.println("发生异常了");
    }
}
